<?php defined('BASEPATH') || exit('No direct script access allowed');

class Area_model extends BF_Model
{
    protected $table_name	= 'areas';
	protected $key			= 'area_id';
	protected $date_format	= 'datetime';

	protected $log_user 	= false;
	protected $set_created	= false;
	protected $set_modified = false;
	protected $soft_deletes	= true;

    protected $deleted_field     = 'deleted';

	// Customize the operations of the model without recreating the insert,
    // update, etc. methods by adding the method names to act as callbacks here.
	protected $before_insert 	= array();
	protected $after_insert 	= array();
	protected $before_update 	= array();
	protected $after_update 	= array();
	protected $before_find 	    = array();
	protected $after_find 		= array();
	protected $before_delete 	= array();
	protected $after_delete 	= array();

	// For performance reasons, you may require your model to NOT return the id
	// of the last inserted row as it is a bit of a slow method. This is
    // primarily helpful when running big loops over data.
	protected $return_insert_id = true;

	// The default type for returned row data.
	protected $return_type = 'object';

	// Items that are always removed from data prior to inserts or updates.
	protected $protected_attributes = array();

	// You may need to move certain rules (like required) into the
	// $insert_validation_rules array and out of the standard validation array.
	// That way it is only required during inserts, not updates which may only
	// be updating a portion of the data.
	protected $validation_rules 		= array(
		array(
			'field' => 'parent_id',
			'label' => 'lang:area_field_parent_id',
			'rules' => 'max_length[6]',
		),
		array(
			'field' => 'area_name',
			'label' => 'lang:area_field_area_name',
			'rules' => 'required|max_length[120]',
		),
		array(
			'field' => 'area_type',
			'label' => 'lang:area_field_area_type',
			'rules' => 'max_length[1]',
		),
	);
	protected $insert_validation_rules  = array();
	protected $skip_validation 			= true;

    /**
     * Constructor
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Find all user records and the associated role information.
     *
     * @return boolean An array of objects with each user's information.
     */
    public function find_all()
    {
        return parent::find_all();
    }

    /**
     * 获取省份
     * @param  int $parentID 父级ID，默认是中国
     * @return mixed            数组或者false或者空
     */
    public function provinces($parentID = 1) {
        $this->db->where(array('parent_id'=>$parentID, $this->deleted_field=>0));
        return $this->find_all();
    }

    /**
     * 获取城市
     * @param  int $parentID 父级ID
     * @return mixed            数组或者false或者空
     */
    public function cities($parentID) {
        $this->db->where(array('parent_id'=>$parentID, $this->deleted_field=>0));
        return $this->find_all();
    }

    /**
     * 获取区县
     * @param  int $parentID 父级ID
     * @return mixed            数组或者false或者空
     */
    public function districts($parentID) {
        $this->db->where(array('parent_id'=>$parentID, $this->deleted_field=>0));
        return $this->find_all();
    }

    public function prep_data($post_data) {
        $data = parent::prep_data($post_data);

        if (!empty($post_data['province_id'])) {
            $parent_id = $post_data['province_id'];
            $data['parent_id'] = $parent_id;
            $data['area_type'] = 2;
        }
        if (!empty($post_data['city_id'])) {
            $parent_id = $post_data['city_id'];
            $data['parent_id'] = $parent_id;
            $data['area_type'] = 3;
        }
        
        $data['active'] = $data['active']=='on' ? 1 : 0;

        return $data;
    }

    /**
     * 通过市获取父级省
     * @param  int $sonID 子级ID
     * @return mixed            数组或者false或者空
     */
    public function getprovince($sonID){
        $province_id=  $this->find($sonID)->parent_id;
        $data = $this->find($province_id);
        return $data;

    }
    /**
     * 所有的市
     * @return mixed            数组或者false或者空
     */
    public function getcities(){
        $this->db->where(array('area_type'=>2, $this->deleted_field=>0));
        return $this->find_all();
    }

}